#!/bin/sh

#
# Copyright (c) 2014, The Linux Foundation. All rights reserved.
#
# Copyright (C) 2020, Tano Systems LLC. All rights reserved.
# Anton Kikin <a.kikin@tano-systems.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of The Linux Foundation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# find_partitions        init.d script to dynamically find partitions
#

source /etc/run.env

MTD_FILE=/proc/mtd
MODEM_PARTITION=modem
DS_MODEM_SUB_SYSTEM_FLAG=000

# Wait until device shows up. After testing, it spent about 6 msec to
# find the devices of /dev/ubi1_0 and /dev/ubiblock1_0. So, limit the
# maximum time spent here to about 60 msec.
wait_on_dev()
{
	local cntmax=20
	local ret=${SWI_OK}
	while [ ! "$1" "$2" ]; do
		# When sleep 3 msec, it actually sleep for 3~4 msec, so here
		# sleep 3 msec every cycle.
		usleep 3000
		cntmax=$( echo $(( ${cntmax} - 1 )) )
		if [ ${cntmax} -eq 0 ]; then
			ret=${SWI_ERR}
			break
		fi
	done
	return ${ret}
}

FindAndMountUBI () {
	local partition=$1
	local dir=$2
	local mtd_block_number=`cat $MTD_FILE | grep -iw $partition | sed 's/^mtd//' | awk -F ':' '{print $1}'`
	mkdir -p $dir

	if ! [ -e "/dev/ubi2_0" ]; then
		ubiattach -m ${mtd_block_number} -d 2
		wait_on_dev "-c" "/dev/ubi2_0"
	fi

	if ! [ -e "/dev/ubiblock2_0" ]; then
		ubiblock --create /dev/ubi2_0
		wait_on_dev "-b" "/dev/ubiblock2_0"
	fi

	local BOOTTYPE="squashfs"
	local BOOTDEV="/dev/ubiblock2_0"
	local BOOTOPTS="ro"

	mount -t ${BOOTTYPE} ${BOOTDEV} $dir -o ${BOOTOPTS}
	if [ $? -ne 0 ] ; then
		echo "swi-preinit: Unable to mount ${BOOTTYPE} onto ${BOOTDEV}" > /dev/kmsg
		return ${SWI_ERR}
	fi
}

do_swi_find_partitions() {
	eval FindAndMountUBI ${MODEM_PARTITION} /firmware
	if [ $? -ne ${SWI_OK} ]; then
		echo "swi-preinit: Failed to mount modem partition" > /dev/kmsg
	fi
}

boot_hook_add preinit_main do_swi_find_partitions
